package com.smart.tool.scheduler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xiaoqiang
 * @Version ScheduleContext.java, v 0.1 2025年08月17日 23:03 xiaoqiang
 * @Description: TODO
 */
public class ScheduleContext<T> {
    private String taskId;
    private String taskKey;

    private TaskType taskType;
    private Throwable throwable;

    private T result;

    private Date startTime;
    private Date endTime;

    private int executionCount; // 新增：执行次数
    private Date nextExecutionTime; // 新增：下次执行时间

    /**
     * 参数缓存，存放任务执行过程中需要的参数
     * 例如：定时任务需要的参数，定时任务的执行结果等。用户自行定义和存储
     */
    private Map<String, Object> metadata = new HashMap<>();

    private ScheduleContext(String taskId, String taskKey, TaskType taskType) {
        this.taskId = taskId;
        this.taskKey = taskKey;
        this.taskType = taskType;
    }

    public static <T> ScheduleContext<T> of(String taskId, String taskKey, TaskType taskType) {
        return new ScheduleContext(taskId, taskKey, taskType);
    }

    public ScheduleContext withStartTime(Date startTime) {
        this.startTime = startTime;
        return this;
    }
    public ScheduleContext withEndTime(Date endTime) {
        this.endTime = endTime;
        return this;
    }

    public ScheduleContext withExecutionCount(int executionCount) {
        this.executionCount = executionCount;
        return this;
    }

    public ScheduleContext withNextExecutionTime(Date nextExecutionTime) {
        this.nextExecutionTime = nextExecutionTime;
        return this;
    }

    public ScheduleContext withResult(T result) {
        this.result = result;
        return this;
    }

    public ScheduleContext withThrowable(Throwable throwable) {
        this.throwable = throwable;
        return this;
    }
    public ScheduleContext withParam(String key, Object value  ) {
        metadata.put(key, value);
        return this;
    }
    public Object getParamValue(String key) {
        return metadata.get(key);
    }


    public String getTaskId() {
        return taskId;
    }

    public String getTaskKey() {
        return taskKey;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public T getResult() {
        return result;
    }

    public Date getStartTime() {
        return startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public int getExecutionCount() {
        return executionCount;
    }

    public Date getNextExecutionTime() {
        return nextExecutionTime;
    }
}
